package com.xinmiao.test8;

import android.app.Service;
import android.graphics.Color;
import android.os.Vibrator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

import java.util.Collections;
import java.util.List;

/**
 * Created by ${王俊强} on 2017/12/3.
 */

public class ItmeTouch<T> extends ItemTouchHelper.Callback {
    private List<T> mTList;
    private Vibrator vibrator;
    private RecyclerView.Adapter mAdapter;

    public ItmeTouch(List<T> tList, RecyclerView.Adapter adapter) {
        mTList = tList;
        mAdapter = adapter;
    }

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        int swFlags = 0;
        int dflags = 0;
        if (layoutManager != null && layoutManager instanceof GridLayoutManager) {
            //拖拽 标志   如果是网格类型的则有UP、DOWN、LEFT、RIGHT四个方向
            dflags = ItemTouchHelper.DOWN |
                    ItemTouchHelper.UP |
                    ItemTouchHelper.LEFT |
                    ItemTouchHelper.RIGHT;
            //滑动 标志 暂时设置为0  不做处理
            swFlags = 0;
        } else if (layoutManager != null && layoutManager instanceof LinearLayoutManager) {
            //拖拽 标志  如果是列表类型的RecyclerView，拖拽只有UP、DOWN两个方向
            dflags = ItemTouchHelper.DOWN |
                    ItemTouchHelper.UP;
            //滑动 标志 暂时设置为0  不做处理
            swFlags = ItemTouchHelper.START |
                    ItemTouchHelper.END;
        }
        return makeMovementFlags(dflags, swFlags);
    }

    @Override
    public boolean onMove(RecyclerView recyclerView,
                          RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        try {


            //得到拖拽itme的viewholder 的position
            int viewHolderPosition = viewHolder.getAdapterPosition();
            //得到拖拽到此处的itme的viewholder 的position
            int targetPosition = target.getAdapterPosition();

            if (viewHolderPosition < targetPosition) {
                for (int i = viewHolderPosition - 1; i < targetPosition - 1; i++) {
                    Collections.swap(mTList, i, i + 1);//依次改变交换数据集
                }
            } else {
                for (int i = viewHolderPosition - 1; i > targetPosition - 1; i--) {
                    Collections.swap(mTList, i, i - 1);//依次改变交换数据集
                }
            }
            mAdapter.notifyItemMoved(viewHolderPosition, targetPosition);

            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override//滑动删除 操作
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        int adapterPosition = viewHolder.getAdapterPosition();
        mAdapter.notifyItemRemoved(adapterPosition);
        mTList.remove(adapterPosition-1);
    }

    @Override//当长按itme选中的时候 拖拽开始的时候调用
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {//不等于空闲
            viewHolder.itemView.setBackgroundColor(Color.BLUE);
            vibrator =//获取震动管理器
                    (Vibrator) viewHolder.itemView.getContext().
                            getSystemService(Service.VIBRATOR_SERVICE);
            if (vibrator != null) {
                vibrator.vibrate(100);
            }
        }
        super.onSelectedChanged(viewHolder, actionState);
    }

    @Override //当手指松开的时候  拖拽完成
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        super.clearView(recyclerView, viewHolder);
        viewHolder.itemView.setBackgroundColor(0);
        if (vibrator != null) {
            vibrator.cancel();
            vibrator = null;
        }
    }
}
